package com.csci;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;


public class Main {	
	File easyPuzzleSolution = new File("C:\\Users\\grebwerd\\Desktop\\easyPuzzleSolution.txt");
	FileWriter fileWriter; 
	BufferedWriter bufferedWriter; 
	public static void main(String[] args) throws IOException{
			
			int totalNumberCalls = 0;
			int numberOfPuzzles = 20;
			Random rand = new Random();
			int percentOfClues = 20;
			int totalNumberOfClues = 0;
			//Main m = new Main();
		    //m.initializePuzzleSolution();
			
			for(int i = 0; i <numberOfPuzzles; i++){
				Sudoku sudoku = new Sudoku();
				sudoku.getPuzzle(i+1, percentOfClues, rand);
				totalNumberOfClues += sudoku.totalClueCounter;
				Display display = new Display(sudoku);
				BackTrackAlgorithm bta = new BackTrackAlgorithm();
				display.displaySudokuPuzzleGrid();
				System.out.println("\n\n");
				Sudoku s = bta.backTrack(sudoku);
				System.out.println("Solution to the puzzle is Done \n\n");
				Display displaySolution = new Display(s);
				//m.writePuzzleToFile(s);
			    displaySolution.displaySudokuPuzzleGrid();
			    System.out.println("For puzzle " + i + ": BackTrack counter is " + bta.backTrackCounter);
			    System.out.println("\n\n\n");
			    totalNumberCalls = bta.backTrackCounter;
			}
			
			System.out.println("The total number of backtracker was " + totalNumberCalls);
			System.out.println("The total number of clues was " + totalNumberOfClues);
			
			float average = ((float)totalNumberCalls/(float)numberOfPuzzles);
			System.out.println("The average number of back track calls per puzzle was " + average);
			
			float averageClues = ((float)totalNumberOfClues/(float)numberOfPuzzles);
			System.out.println("The average number of clues per puzzle was " + averageClues);	    
	}
	
	public void initializePuzzleSolution() {
		try {
			fileWriter = new FileWriter(easyPuzzleSolution.getAbsoluteFile(), true);
			bufferedWriter = new BufferedWriter(fileWriter);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void writePuzzleToFile(Sudoku s) throws IOException {
		for(int row =0; row < 9; row++){
			for(int col = 0; col < 9; col++){
				try {
					bufferedWriter.write(Integer.toString(s.puzzleGrid[row][col]));
					bufferedWriter.flush();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		bufferedWriter.write('\n');
		System.out.println("Successfully wrote the solution to a file");
	}
	
}
